//
//  CollectionViewPackage.m
//  封装轮播图
//
//  Created by 甘霞 on 15/11/23.
//  Copyright © 2015年 甘霞. All rights reserved.
//

#import "CollectionViewPackage.h"
#import "CollectionViewCell.h"
#import "GXSuggestModel.h"

@interface CollectionViewPackage () <UICollectionViewDataSource, UICollectionViewDelegate>
@end

@implementation CollectionViewPackage

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubViews];
    }
    return self;
}

- (void)createSubViews {
    //  瀑布流
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    //  itemSize
    layout.itemSize = CGSizeMake(self.frame.size.width, self.frame.size.height);
    //  列间距
    layout.minimumLineSpacing = 0;
    //  行间距
    layout.minimumInteritemSpacing = 0;
    //  滚动方向
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    //  滚动视图
    self.collectionView = [[UICollectionView alloc] initWithFrame:self.bounds
                                             collectionViewLayout:layout];
    //  允许被选中
    self.collectionView.allowsSelection = YES;
    //  分页滚动
    self.collectionView.pagingEnabled = YES;
    //  代理
    self.collectionView.delegate = self;
    //  数据源
    self.collectionView.dataSource = self;
    //  加到父控件
    [self addSubview:self.collectionView];
    //  注册 cell
    [self.collectionView registerClass:[CollectionViewCell class]
            forCellWithReuseIdentifier:@"cell"];
}

#pragma mark - 子控件布局
- (void)layoutSubviews {
    //  滚动视图布局与父控件相同
    self.collectionView.frame = self.bounds;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 100;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView
     numberOfItemsInSection:(NSInteger)section {
    return self.dataArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
                  cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell"
                                                                         forIndexPath:indexPath];
    cell.suggestModel = self.dataArr[indexPath.item];
    
    //  每次滚动页面时 更换页码显示
    [[NSNotificationCenter defaultCenter] postNotificationName:@"pageChange"
                                                        object:nil
                                                      userInfo:@{@"currentPage" : [NSNumber numberWithInteger:indexPath.item + 1], @"totalPage" : [NSNumber numberWithInteger:self.dataArr.count]}];
    
    return cell;
}

#pragma mark - 开启定时器
- (void)openTimer {
    
    self.timer = [NSTimer scheduledTimerWithTimeInterval:5.0f
                                                  target:self
                                                selector:@selector(timerAction:)
                                                userInfo:nil
                                                 repeats:YES];
    
    [[NSRunLoop mainRunLoop] addTimer:self.timer
                              forMode:NSRunLoopCommonModes];
}

#pragma mark - 定时器开启响应方法
- (void)timerAction:(NSTimer *)timer {
    
    //  获取当前视图上得item
    NSIndexPath *indexPath = [self.collectionView indexPathsForVisibleItems][0];
    
    //  下一个要滚动的
    NSInteger nextItem = indexPath.item + 1;
    NSInteger nextSection = indexPath.section;
    
    //  防止数组越界
    if (nextItem == self.dataArr.count) {
        
        nextItem = 0;
        nextSection++;
        
        if (nextSection == 100) {
            nextSection = 0;
        }
    }
    
    //  滚
    NSIndexPath *nextIndexPath = [NSIndexPath indexPathForItem:nextItem
                                                     inSection:nextSection];
    
    [self.collectionView scrollToItemAtIndexPath:nextIndexPath
                                atScrollPosition:UICollectionViewScrollPositionLeft
                                        animated:YES];
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    NSLog(@"❤️%ld",(long)indexPath.item);
    //  代理实现点击方法
    [self.delegate collectionViewDidSelectItemAtIndexPath:indexPath];
}

#pragma mark - 关闭定时器
- (void)timerInvalidate {
    [self.timer invalidate];
    self.timer = nil;
}

#pragma mark - 开始拖拽 关闭定时器
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    [self timerInvalidate];
}

#pragma mark - 结束拖拽 开启定时器
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView
                  willDecelerate:(BOOL)decelerate {
    [self openTimer];
}

@end
